//
//  ZCWelcomeViewController.swift
//  weibo
//
//  Created by Ning Xie on 16/8/11.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class ZCWelcomeViewController: UIViewController {

    private lazy var bgImageView:UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    private lazy var iconImageView:UIImageView = {
        let icon = UIImageView()
        icon.layer.cornerRadius = 30
        icon.layer.masksToBounds = true
        
        if let iconName = ZCUserInfoViewModel.sharedUserInfo.userInfo?.avatar_large{
            icon.sd_setImageWithURL(NSURL(string:iconName), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        return icon
        
    }()
    
    private lazy var welcomeLab:UILabel = {
        let label = UILabel()
        
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(14)
        
        
        if let name = ZCUserInfoViewModel.sharedUserInfo.userInfo?.name {
            label.text = "欢迎回来, \(name)"
        } else {
            label.text = "欢迎回来"
        }
        
        return label
    }()
    
    override func loadView() {
        
        view = bgImageView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        
    }
    override func viewDidAppear(animated: Bool) {
        
        super.viewDidAppear(animated)
        
        startAnimation()
    }
    
    
    private func setupUI(){
        
        view.addSubview(iconImageView)

        view.addSubview(welcomeLab)
        
        
        iconImageView.snp_makeConstraints { (make) in
           make.centerX.equalTo(view)
            make.top.equalTo(view).offset(300)
            make.size.equalTo(CGSize(width: 60, height: 60))
            
        }
        welcomeLab.snp_makeConstraints { (make) in
            make.centerX.equalTo(iconImageView)
            make.top.equalTo(iconImageView.snp_bottom).offset(10)
            
        }
        
    }
    private func startAnimation(){
        
        welcomeLab.alpha = 0
        iconImageView.snp_makeConstraints { (make) in
            make.top.equalTo(view).offset(200)
        }
        
        UIView.animateWithDuration(2, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0, options: [], animations: {
            
            self.view.layoutIfNeeded()
            
        }) { (_) in
            UIView.animateWithDuration(2, animations: {
                
                self.welcomeLab.alpha = 1
                
                }, completion: { (_) in
                    NSThread.sleepForTimeInterval(1)
                    NSNotificationCenter.defaultCenter().postNotificationName(exchangeRootVC, object: self)
            })
            
        }
        
    }
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
}
